package cn.bjc.user.controller;


import cn.bjc.basic.exception.BusinessException;
import cn.bjc.basic.util.JsonResult;
import cn.bjc.user.dto.LoginDto;
import cn.bjc.user.dto.WxDto;
import cn.bjc.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    
    @Autowired
    private ILoginService loginService;

    /**
     * 有三个参数
     * account: 'admin',
     * password: '123456',
     * type: 0,  
     */
    @PostMapping("/account")
    public JsonResult account(@RequestBody LoginDto dto){
        try {
            //service层
            //return loginService.account(dto);
            Map<String,Object> map = loginService.account(dto);
            //返回一个map给JsonResult.me
            return JsonResult.me().setResultObj(map);
            
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙!");
        }
    }
    
    //微信登录
    @GetMapping("/weChat/{code}")
    public JsonResult weChat(@PathVariable("code") String code){
        try {
            
            //service直接返回jsonResult
            return loginService.weChat(code);

        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙!");
        }
    }

    //微信绑定
    @PostMapping("/wechat/binder")
    public JsonResult binder(@RequestBody WxDto dto){
        try {

            //service直接返回jsonResult
            return loginService.binder(dto);

        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙!");
        }
    }
}
